package com.itheima.controller;


import com.itheima.anno.Log;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.EmpQueryParam;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {

    @Autowired
    private ClazzService clazzService;


    @GetMapping
    public Result page(EmpQueryParam empQueryParam){
        log.info("分页查询:{}",empQueryParam);
        PageBean pageBean = clazzService.page(empQueryParam);

        return Result.success(pageBean);

    }

    @Log
    @PostMapping
    public Result add(@RequestBody Clazz clazz){
        log.info("添加班级:{}",clazz);
        clazzService.add(clazz);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        log.info("查询Id为:{}",id);
        Clazz cla = clazzService.getInfo(id);
        return Result.success(cla);
    }

    @Log
    @PutMapping
    public Result modify(@RequestBody Clazz clazz){
        log.info("修改的数据为:{}",clazz);
        clazzService.modify(clazz);
        return Result.success();
    }

    @Log
    @DeleteMapping("/{id}")
    public Result Delete(@PathVariable Integer id){
//        Integer i = clazzService.getCount(id);
//        if(i>0){
//            return Result.error("该班级有学生,无法删除");
//        }else {
//            clazzService.delete(id);
//            return Result.success();
//        }
        return clazzService.delete(id);
    }

    @GetMapping("/list")
    public Result getAll(){
        List<Clazz> list = clazzService.getAll();
        return Result.success(list);
    }
}
